package com.ruoyi.ruoyiserver.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;


@Component
public class AutoFillConfig implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.fillStrategy(metaObject, "createTime", new Date());
        //存放当前 登入的user
        String createUserName = null;
        try {
            createUserName = SecurityUtils.getUsername();
        } catch (Exception e) {

        }
        // this.fillStrategy(metaObject, "createUserId", SecurityUtils.getUserId());
        this.fillStrategy(metaObject, "createUser", createUserName);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date()); // 起始版本 3.3.0(推荐)
        this.fillStrategy(metaObject, "updateUserId", SecurityUtils.getUserId());
    }
}
